﻿// jPaginate Plugin for jQuery - Version 0.3
// by Angel Grablev for Enavu Web Development network (enavu.com)
// Dual license under MIT and GPL :) enjoy
/*

To use simply call .paginate() on the element you wish like so:
$("#content").jPaginate(); 

you can specify the following options:
items = number of items to have per page on pagination
next = the text you want to have inside the text button
previous = the text you want in the previous button
active = the class you want the active paginaiton link to have
pagination_class = the class of the pagination element that is being generated for you to style
minimize = minimizing will limit the overall number of elements in the pagination links
nav_items = when minimize is set to true you can specify how many items to show
cookies = if you want to use cookies to remember which page the user is on, true by default
position = specify the position of the pagination, possible options: "before", "after", or "both"
equal = implements an equal height main element by using the highest possible element use true false
offset = unfortunately calculating heights with javascript isn't always 100% accurate, so please use this value to make it perfect :) its defaultly set to 50

*/
(function ($) {
    $.fn.jPaginate = function (options) {
        var defaults = {
            items: 5,
            next: "Next →",
            previous: "← Previous",
            active: "active",
            pagination_class: "pagination",
            minimize: false,
            nav_items: 6,
            cookies: false,
            position: "after",
            equal: false,
            offset: 50
        };
        var options = $.extend(defaults, options);

        return this.each(function () {
            // object is the selected pagination element list
            var obj = $(this);
            var index = parseInt(obj.parent().attr("index"), 10);
            // this is how you call the option passed in by plugin of items
            var show_per_page = options.items;
            //getting the amount of elements inside parent element
            var number_of_items = obj.children().size();
            //calculate the number of pages we are going to have
            var number_of_pages = Math.ceil(number_of_items / show_per_page);
            //create the pages of the pagination

            var numP = 0;
            var nexP = show_per_page;

            var height = 0;
            var max_height = 0;
            //loop through all pages and assign elements into array
            array_of_elements[index] = [];
            for (i = 1; i <= number_of_pages; i++) {
                array_of_elements[index][i] = obj.children().slice(numP, nexP);

                if (options.equal) {
                    obj.children().slice(numP, nexP).each(function () {
                        height += $(this).outerHeight();
                    });
                    if (height > max_height) max_height = height;
                    height = 0;
                }

                numP += show_per_page;
                nexP += show_per_page;
            }
            if (options.equal) {
                max_height += options.offset;
                obj.css({ "height": max_height });
            }

            if (number_of_items > 0 && number_of_pages>1) {
                showPage(obj, 1);
                createPagination(obj, 1);
            }
            //show selected page
            function showPage(obj, page) {
                obj.children().hide();
                array_of_elements[obj.parent().attr("index")][page].show();
            }

            // create the navigation for the pagination 
            function createPagination(obj, curr) {
                var start, items = "", end, nav = "";
                var number_of_pages = array_of_elements[obj.parent().attr("index")].length - 1;
                start = "<div class='" + options.pagination_class + "' id='" + obj.parent().attr("id") + "pager'><ul>";
                var previous = "<li><a class='goto_previous' pagerid='" + obj.parent().attr("id") + "' href='#'>" + options.previous + "</a></li>";
                var next = "<li><a class='goto_next' pagerid='" + obj.parent().attr("id") + "' href='#'>" + options.next + "</a></li>";
                var previous_inactive = "<li><a class='inactive'>" + options.previous + "</a></li>";
                var next_inactive = "<li><a class='inactive'>" + options.next + "</a></li>";
                previous = "";
                next = "";
                previous_inactive = "";
                next_inactive = "";
                end = "</ul><br clear='all' /></div>"
                var after = number_of_pages - options.after + 1;
                var pagi_range = paginationCalculator(curr);
                for (i = 1; i <= number_of_pages; i++) {
                    if (options.minimize == true) {
                        var half = Math.ceil(number_of_pages / 2)
                        if (i >= pagi_range.start && i <= pagi_range.end) {
                            if (i == curr) { items += '<li><a class="' + options.active + '" title="' + i + '">' + i + '</a></li>'; }
                            else { items += '<li><a href="#" class="goto" title="' + i + '" pagerid="' + obj.parent().attr("id") + '">' + i + '</a></li>'; }
                        } else if (curr <= half) {
                            if (i >= (number_of_pages - 2)) {
                                if (i == curr) { items += '<li><a class="' + options.active + '" title="' + i + '">' + i + '</a></li>'; }
                                else { items += '<li><a href="#" class="goto" title="' + i + '" pagerid="' + obj.parent().attr("id") + '">' + i + '</a></li>'; }
                            }
                        } else if (curr >= half) {
                            if (i <= 2) {
                                if (i == curr) { items += '<li><a class="' + options.active + '" title="' + i + '">' + i + '</a></li>'; }
                                else { items += '<li><a href="#" class="goto" title="' + i + '" pagerid="' + obj.parent().attr("id") + '">' + i + '</a></li>'; }
                            }
                        }
                    } else {
                        if (i == curr) { items += '<li><a class="' + options.active + '" title="' + i + '">' + i + '</a></li>'; }
                        else { items += '<li><a href="#" class="goto" title="' + i + '" pagerid="' + obj.parent().attr("id") + '">' + i + '</a></li>'; }
                    }
                }
                if (curr != 1 && curr != number_of_pages) {
                    nav = start + previous + items + next + end;
                } else if (number_of_pages == 1) {
                    nav = start + previous_inactive + items + next_inactive + end;
                } else if (curr == number_of_pages) {
                    nav = start + previous + items + next_inactive + end;
                } else if (curr == 1) {
                    nav = start + previous_inactive + items + next + end;
                }
                if (options.position == "before") {
                    obj.before(nav);
                } else if (options.position == "after") {
                    obj.parent().after(nav);
                } else {
                    obj.after(nav);
                    obj.before(nav)
                }

            }



            function paginationCalculator(curr) {
                var half = Math.floor(options.nav_items / 2);
                var upper_limit = number_of_pages - options.nav_items;
                var start = curr > half ? Math.max(Math.min(curr - half, upper_limit), 0) : 0;
                var end = curr > half ? Math.min(curr + half + (options.nav_items % 2), number_of_pages) : Math.min(options.nav_items, number_of_pages);
                return { start: start, end: end };
            }

            // handle click on pagination 
            $(".goto").live("click", function (e) {
                e.preventDefault();
                var obj = $("#" + $(this).attr("pagerid") + " tbody")
                showPage(obj, $(this).attr("title"));
                $("#" + $(this).attr("pagerid") + 'pager').remove();
                createPagination(obj, $(this).attr("title"));
            });
            $(".goto_next").live("click", function (e) {
                e.preventDefault();
                var obj = $("#" + $(this).attr("pagerid") + " tbody")
                var pager = $("#" + $(this).attr("pagerid") + "pager");

                var newcurr = parseInt(pager.find(".active").attr("title")) + 1;
                showPage(obj, newcurr);
                $("#" + $(this).attr("pagerid") + 'pager').remove();
                createPagination(obj, newcurr);

            });
            $(".goto_previous").live("click", function (e) {
                e.preventDefault();
                var obj = $("#" + $(this).attr("pagerid") + " tbody")
                var pager = $("#" + $(this).attr("pagerid") + "pager");
                var newcurr = parseInt(pager.find(".active").attr("title")) - 1;
                showPage(obj, newcurr);
                $("#" + $(this).attr("pagerid") + 'pager').remove();
                createPagination(obj, newcurr);
            });
        });


    };
})(jQuery);